package com.youi.web;

import java.text.ParseException;
import java.util.Date;
import java.util.List;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.youi.exception.BusinessException;
import com.youi.manage.HttpRequestManage;
import com.youi.common.DelStatus;
import com.youi.service.MerchantService;
import com.youi.entity.Merchant;
import com.youi.common.Common;
import com.youi.util.CommonInfoUtil;
import com.youi.util.DateUtil;
import com.youi.util.JsonUtil;
import com.youi.util.ObjectUtil;
import com.youi.util.Page;

/**
* Merchant控制层
* @author Administrator
*/
@Controller
public class MerchantController  extends HttpRequestManage{

		@Autowired
		private MerchantService merchantService;

		/**
		 *添加或编辑Merchant
		*/
		@ResponseBody
		@RequestMapping("addOrEditMerchant")
		public String addOrEditMerchant(ModelMap modelMap,HttpServletRequest request)throws BusinessException,ParseException{
			Long id=optLong("id",request);
			Merchant merchant=null;
			if (id!=null) {
				merchant=merchantService.get(id);
			}else {
				merchant=new Merchant();
				merchant.setDelStatus(DelStatus.NODEL);
				merchant.setGmtCreated(new Date());
			}
			Long merchantGroupId=optLong("merchantGroupId",request);
			if (merchantGroupId!=null){
				merchant.setMerchantGroupId(merchantGroupId);
			}
			Long merchantClassId=optLong("merchantClassId",request);
			if (merchantClassId!=null){
				merchant.setMerchantClassId(merchantClassId);
			}
			Integer status=optInteger("status",request);
			if (status!=null){
				merchant.setStatus(status);
			}
			String name=optString("name",request);
			if (name!=null){
				merchant.setName(name);
			}
			String shortName=optString("shortName",request);
			if (shortName!=null){
				merchant.setShortName(shortName);
			}
			String merchantCode=optString("merchantCode",request);
			if (merchantCode!=null){
				merchant.setMerchantCode(merchantCode);
			}
			Integer nature=optInteger("nature",request);
			if (nature!=null){
				merchant.setNature(nature);
			}
			String project=optString("project",request);
			if (project!=null){
				merchant.setProject(project);
			}
			String ranges=optString("ranges",request);
			if (ranges!=null){
				merchant.setRanges(ranges);
			}
			String province=optString("province",request);
			if (province!=null){
				merchant.setProvince(province);
			}
			String city=optString("city",request);
			if (city!=null){
				merchant.setCity(city);
			}
			String district=optString("district",request);
			if (district!=null){
				merchant.setDistrict(district);
			}
			String contacts=optString("contacts",request);
			if (contacts!=null){
				merchant.setContacts(contacts);
			}
			String phone=optString("phone",request);
			if (phone!=null){
				merchant.setPhone(phone);
			}
			String email=optString("email",request);
			if (email!=null){
				merchant.setEmail(email);
			}
			String introduce=optString("introduce",request);
			if (introduce!=null){
				merchant.setIntroduce(introduce);
			}
			String address=optString("address",request);
			if (address!=null){
				merchant.setAddress(address);
			}
			String merchantPhone=optString("merchantPhone",request);
			if (merchantPhone!=null){
				merchant.setMerchantPhone(merchantPhone);
			}
			Integer isRecommend=optInteger("isRecommend",request);
			if (isRecommend!=null){
				merchant.setIsRecommend(isRecommend);
			}
			String brandName=optString("brandName",request);
			if (brandName!=null){
				merchant.setBrandName(brandName);
			}
			String brandIntroduce=optString("brandIntroduce",request);
			if (brandIntroduce!=null){
				merchant.setBrandIntroduce(brandIntroduce);
			}
			String account=optString("account",request);
			if (account!=null){
				merchant.setAccount(account);
			}
			String password=optString("password",request);
			if (password!=null){
				merchant.setPassword(password);
			}
			Integer accountNum=optInteger("accountNum",request);
			if (accountNum!=null){
				merchant.setAccountNum(accountNum);
			}
			Integer goodsNum=optInteger("goodsNum",request);
			if (goodsNum!=null){
				merchant.setGoodsNum(goodsNum);
			}
			Double commissionRatio=optDouble("commissionRatio",request);
			if (commissionRatio!=null){
				merchant.setCommissionRatio(commissionRatio);
			}
			String businessLicense=optString("businessLicense",request);
			if (businessLicense!=null){
				merchant.setBusinessLicense(businessLicense);
			}
			String gapMsg=optString("gapMsg",request);
			if (gapMsg!=null){
				merchant.setGapMsg(gapMsg);
			}
			String gspMsg=optString("gspMsg",request);
			if (gspMsg!=null){
				merchant.setGspMsg(gspMsg);
			}
			String idtscMsg=optString("idtscMsg",request);
			if (idtscMsg!=null){
				merchant.setIdtscMsg(idtscMsg);
			}
			String idisqcMsg=optString("idisqcMsg",request);
			if (idisqcMsg!=null){
				merchant.setIdisqcMsg(idisqcMsg);
			}
			merchant.setGmtModify(new Date());
			merchantService.saveOrUpdate(merchant);
			return JsonUtil.toJson(CommonInfoUtil.SUCCESS, "操作完成");
		}

		/**
		 *批量删除Merchant
		*/
		@ResponseBody
		@RequestMapping("delMerchantByIds")
		public String delMerchantByIds(ModelMap modelMap,HttpServletRequest request) throws BusinessException{
			String ids=getString("ids",request);
			List<Long> idsList=ObjectUtil.getIdListByIds(ids);
			if (idsList==null||idsList.size()==0) {
				return JsonUtil.toJson(CommonInfoUtil.PARAMERROR, "ids参数错误");
			}
			Map<String, Object> params=new HashMap<String, Object>();
			params.put(Common.DELSTATUS, DelStatus.NODEL);
			params.put(Common.ID, idsList);
			List<Merchant> merchantList= merchantService.getByParams(params);
			if (merchantList==null||merchantList.size()==0) {
				return JsonUtil.toJson(CommonInfoUtil.NULLDATA, "空数据");
			}
			for (Merchant merchant : merchantList) {
				merchant.setDelStatus(DelStatus.DEL);
			}
			merchantService.saveOrUpdateAll(merchantList);
			return JsonUtil.toJson(CommonInfoUtil.SUCCESS, "操作完成");
		}

		/**
		 * 分页查询Merchant
		*/
		@ResponseBody
		@RequestMapping("getMerchant")
		public String getMerchant(ModelMap modelMap,HttpServletRequest request) throws BusinessException,ParseException{
			Integer pageIndex=optInteger("pageIndex",request,1);
			Integer pageNum=optInteger("pageNum",request,10);
			String des=optString("des",request,Common.DESC);
			String pms=optString("pms",request);
			Date start=optDate("start",request,"yyyy-MM-dd HH:mm:ss");
			Date end=optDate("end",request,"yyyy-MM-dd HH:mm:ss");
			Map<String, Object> prams=new HashMap<String, Object>();
			prams.put(Common.DELSTATUS, DelStatus.NODEL);
			Map<String, Object> sortPram=new HashMap<String, Object>();
			sortPram.put(Common.ID,des);
			Map<String, Object> searchPram=null;
			if (pms!=null) {
				searchPram=new HashMap<String, Object>();
			}
			Map<String, Object> startTime=null;
			if(start!=null){
				startTime=new HashMap<String, Object>();
				startTime.put(Common.GMTCREATED,start);
			}
			Map<String, Object> endTime=null;
			if(end!=null){
				endTime=new HashMap<String, Object>();
				endTime.put(Common.GMTCREATED,end);
			}
			List<Merchant> merchantList=merchantService.getByParamsList(prams, searchPram,sortPram,  (pageIndex-1)*pageNum, pageNum, startTime, endTime);
			if (merchantList==null||merchantList.size()==0) {
				return JsonUtil.toJson(CommonInfoUtil.NULLDATA, "空数据");
			}
			Integer pageCount=merchantService.getCountByParamss(prams, searchPram, startTime, endTime);
			modelMap.put(CommonInfoUtil.PAGECOUNT, Page.getCount(pageCount, pageNum));//pageCount  总页数
			modelMap.put(CommonInfoUtil.PAGEINDEX, pageIndex);			//pageIndex     查询页数
			modelMap.put(CommonInfoUtil.ALLCOUINT,pageCount);
			modelMap.put(CommonInfoUtil.DATA, merchantList);		//jsonObjectList json对象集合
			return JsonUtil.toJson(CommonInfoUtil.SUCCESS, "操作完成",modelMap);
		}

		/**
		 *根据id查询Merchant
		*/
		@ResponseBody
		@RequestMapping("getMerchantById")
		public String getMerchantById(ModelMap modelMap,HttpServletRequest request) throws BusinessException{
			Long id=getLong("id",request);
			Merchant merchant=merchantService.get(id);
			if (merchant==null) {
				return JsonUtil.toJson(CommonInfoUtil.NULLDATA, "空数据");
			}
			return JsonUtil.toJson(CommonInfoUtil.SUCCESS, "操作完成",merchant);//jsonObject	json对象
		}

		/**
		 * 查询所有Merchant
		 * @param modelMap
		 * @param session
		 * @return
		 */
		@ResponseBody
		@RequestMapping("getAllMerchant")
		public String getAllMerchant(ModelMap modelMap,HttpServletRequest request) throws BusinessException{
			List<Merchant> merchantList= merchantService.getByParams(null);
			if (merchantList==null||merchantList.size()==0) {
				return JsonUtil.toJson(CommonInfoUtil.NULLDATA, "空数据");
			}
			return JsonUtil.toJson(CommonInfoUtil.SUCCESS, "操作完成",merchantList);//jsonObjectList	json对象集合
		}
}
